package cn.chenxinjie.hrm.filter;

import org.springframework.util.Assert;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 陈鑫杰
 * @date 2022/3/21 11:12
 */
public class SmsCodeFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        // 获取请求里面的uri
        String uri = request.getRequestURI();
        // 拦截/smslogin请求
        if ("/smslogin".equals(uri)) {
            String phone = request.getParameter("phone");
            String code = request.getParameter("code");
            Assert.notNull(code, "验证码不能为空");
            // 获取请求里面的验证码
            String smsCode = (String) request.getSession().getAttribute("sms_code" + phone);
            Assert.isTrue(code.equals(smsCode), "验证码错误");
        }
        // 放行
        filterChain.doFilter(request, response);
    }
}
